package com.moyun.zbmy.zizhou.ui.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.canyinghao.canrefresh.CanRefreshLayout;
import com.moyun.zbmy.zizhou.beans.AccountTagBean;
import com.moyun.zbmy.zizhou.http.AbsListNetCallback;
import com.ruihang.generalibrary.utils.ListUtils;
import com.moyun.zbmy.zizhou.R;
import com.moyun.zbmy.zizhou.application.Constances;
import com.moyun.zbmy.zizhou.base.BaseAccountActivity;
import com.moyun.zbmy.zizhou.beans.AccountBean;
import com.moyun.zbmy.zizhou.http.AbsNetCallBack;
import com.moyun.zbmy.zizhou.http.NetUtils;
import com.moyun.zbmy.zizhou.ui.adapter.MyAttentionAdapter;
import com.moyun.zbmy.zizhou.ui.adapter.holder.MyAttentionHolder;
import com.moyun.zbmy.zizhou.ui.util.IListShowData;
import com.moyun.zbmy.zizhou.ui.utils.DataUtils;
import com.moyun.zbmy.zizhou.ui.utils.TitleUtils;
import com.moyun.zbmy.zizhou.utils.UserManager;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.RequestBody;

/**
 * 我的订阅（已关注的公众号）
 */
public class MyAttentionActivity extends BaseAccountActivity implements CanRefreshLayout.OnRefreshListener, CanRefreshLayout.OnLoadMoreListener, MyAttentionHolder.OnAttentionItemClickListener {

    @BindView(R.id.to_attention_more)
    protected LinearLayout to_attention_more;
    @BindView(R.id.refresh_layout)
    protected CanRefreshLayout mRefreshLayout;
    @BindView(R.id.can_content_view)
    protected RecyclerView mRecyclerView;

    private MyAttentionAdapter adapter;
    private int mPageNo = 1;
    private boolean mIsLoading = false;
    private int mPageAll = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new TitleUtils(this).useAndSetTxTitle("我的关注");
        initView();
    }

    @Override
    protected void initInject() {
        setContentView(R.layout.activity_my_attention);
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initView() {
        mRefreshLayout.setOnRefreshListener(this);
        mRefreshLayout.setOnLoadMoreListener(this);
        mRefreshLayout.setLoadMoreEnabled(false);
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRefreshLayout.autoRefresh();
    }

    @Override
    public void onLoadMore() {
        mPageNo++;
        getData();
    }

    @Override
    public void onRefresh() {
        if (mIsLoading) {
            return;
        }
        mPageNo = 1;
        getData();
    }

    private void getData() {
        if (mIsLoading) {
            onNetFinish();
            return;
        }
        if (null != UserManager.getInstance().getUser()) {
            String token = "";
            String userId = "";
            if (null != UserManager.getInstance().getUser()) {
                token = UserManager.getInstance().getUser().getToken();
                userId = UserManager.getInstance().getUser().getUserId();
            }
            mIsLoading = true;
            Map<String, Object> param = new HashMap<>();
            param.put("siteNumber", Constances.SITE_ID);
            param.put("nodeId", "2069");
            param.put("userId", userId);
            param.put("token", token);
            param.put("type", 1);
            NetUtils.getNetAdapter().getAttentionAccountList(getOwnerName(), param, new AbsListNetCallback<AccountBean>(null) {
                @Override
                public void onSuc(List<AccountBean> list) {
                    initData(list);
                }

                @Override
                public void onError(Throwable ex, String errorMsg) {
                    toast(errorMsg);

                }

                @Override
                public void onEnd() {
                    onNetFinish();
                    mIsLoading = false;
                }
            });
        } else {
            onNetFinish();
        }

    }

    private void initData(List<AccountBean> newsItems) {
        if (null == adapter) {
            adapter = new MyAttentionAdapter(this, newsItems, this);
            mRecyclerView.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else {
            adapter.setData(newsItems);
        }

    }

    private synchronized void onNetFinish() {
        mIsLoading = false;
        mRefreshLayout.refreshComplete();
        mRefreshLayout.loadMoreComplete();
        mRefreshLayout.setLoadMoreEnabled(mPageNo >= mPageAll ? false : true);

    }

    @Override
    public void OnAttentionItemClick(AccountBean temp) {
        Intent intent = new Intent(this, AccountDetailActivity.class);
        intent.putExtra(AccountDetailActivity.ACCOUNT_ID, temp.getFollowedId());
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == 1) {
                mRefreshLayout.autoRefresh();
            }
        }
    }

    @OnClick(value = {R.id.title_top_back, R.id.to_attention_more})
    public void OnClick(View view) {
        switch (view.getId()) {
            case R.id.title_top_back:
                onBackPressed();
                break;
            case R.id.to_attention_more:
                Intent intent = new Intent(this, MoreAccountActivity.class);
                startActivityForResult(intent, 1);
                break;
        }
    }


}
